﻿using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO; 
using System.Text;
using MyLib;
 
namespace PerformanceReport.AjaxPage
{
    /// <summary>
    /// GenerateReports 的摘要说明
    /// </summary>
    public class GenerateDosageReports : IHttpHandler 
    {
        string strSql;
        string strJson;
        string strPara;
        string strStartDate;
        string strEndDate;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            strPara = context.Request["Para"];               //流程名
            strStartDate = context.Request["StartDate"];     //开始时间
            strEndDate = context.Request["EndDate"];       //结束时间
            strSql = "select count(INCIDENT) as INCIDENT,SUM(case when status=2 then 1 else 0 end) as Completed,"
                  + "SUM(case when status=1 then 1 else 0 end) as UnFinished"
                  + " ,SUM(case when status=4 then 1 else 0 end) as Cancelled from  Incidents";
                 // + " where ProcessName like N'%" + strPara.Trim() + "%'";
            string where = " where 1=1 ";
            where += " and ProcessName like N'%" + strPara.Trim() + "%'";
            if (!string.IsNullOrEmpty(strStartDate))
            {
                //where += " and  starttime >= to_date('" + strStartDate.Trim() + "','yyyy-MM-dd')";
                string NowStrData = strStartDate.Trim();
                DateTime dNowStrData = Convert.ToDateTime(NowStrData).AddMonths(-3);
                string strRetult = dNowStrData.ToString("yyyy-MM-dd");
                where += "  and starttime between to_date('" + strRetult.Trim() + "','yyyy-MM-dd') and to_date('" + strStartDate.Trim() + "','yyyy-MM-dd')";
            }
            if (!string.IsNullOrEmpty(strEndDate))
            {
                //where += " and  starttime <= to_date('" + strEndDate.Trim() + "','yyyy-MM-dd')";
            }
            string StrSqlTotal = strSql + where;
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSqlTotal.ToString());

             //如果存在返回值的情况
             if (dt != null)
             {
                 strJson = "[";

                 //循环所有的行
                 for (int i = 0; i < dt.Rows.Count; i++)
                 {
                     strJson += "{'INCIDENT':'" + CheckText(dt.Rows[i]["INCIDENT"].ToString().Trim()) + "',"
                              + "'Completed':'" + CheckText(dt.Rows[i]["Completed"].ToString().Trim()) + "',"
                              + "'Cancelled':'" + CheckText(dt.Rows[i]["Cancelled"].ToString().Trim()) + "',"
                              + "'UnFinished':'" + CheckText(dt.Rows[i]["UnFinished"].ToString().Trim()) + "'},";                 
                 }
                 strJson = strJson.Substring(0, strJson.LastIndexOf(','));
                 strJson += "]";
             }

             context.Response.Buffer = true;
             context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
             context.Response.Expires = 0;
             context.Response.CacheControl = "no-cache";
             context.Response.Write(strJson);
        }

        public string CheckText(string Str)
        {
            string ReturnString = "";
            ReturnString = Str.Replace(",", "#dou#");
            ReturnString = Str.Replace("'", "#dan#");
            return ReturnString;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
         
    }
}